﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.BussnessLogicLayer;
using Capstone_project.DatabaseAccessLayer;

namespace Capstone_project
{
    public partial class frmSearchStaff : Form
    {

        StaffBUS staff = new StaffBUS();
        frmAddNewStaff newstaff = new frmAddNewStaff();
        SqlDataAdapter adap;
        SqlCommand cmd;
        SqlConnection con = Common.con;
        DataTable dt = new DataTable();
        public frmSearchStaff()
        {
            InitializeComponent();
        }

        private void frmSearchStaff_Load(object sender, EventArgs e)
        {
            rbtAll.Checked = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            cmd = new SqlCommand("showAllStaff",con);
            cmd.CommandType = CommandType.StoredProcedure;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            BindingSource bdsource = new BindingSource();
            bdsource.DataSource = dt;
            dataGridView1.DataSource = bdsource;
            bindingNavigator1.BindingSource = bdsource;
            dataGridView1.CurrentCell.Selected = true;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i % 2 == 0)
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                }
            }
        }

        private void txtInformation_TextChanged(object sender, EventArgs e)
        {
            if (rbtUsername.Checked == true)
            {
                rbtUsername_CheckedChanged(null,null);
            }
            else if (rbtFullname.Checked == true)
            {
                rbtFullname_CheckedChanged(null, null);
            }
            else if (rbtAddress.Checked == true)
            {
                rbtAddress_CheckedChanged(null,null);
            }
        }

        private void rbtAll_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = false;
            txtInformation.Text = "";
            frmSearchStaff_Load(null, null);
        }

        private void rbtUsername_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            cmd = new SqlCommand("FindStaffbyUsername", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value= txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void rbtFullname_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            cmd = new SqlCommand("FindStaffbyFullname", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value = txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void rbtAddress_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            cmd = new SqlCommand("FindStaffbyAddress", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value = txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void bindingNavigatorExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    string currentUsername = dataGridView1.CurrentCell.Value.ToString();
        //    this.Hide();
        //    frmPersonalInformation pi = new frmPersonalInformation();
        //    dt=pi.viewPersonalInformation(currentUsername);
        //    string usname = dt.Rows[0][0].ToString();
        //    string fname = dt.Rows[0][1].ToString();
        //    string address = dt.Rows[0][2].ToString();
        //    DateTime bd = Convert.ToDateTime(dt.Rows[0][3]);
        //    string ph = dt.Rows[0][4].ToString();
        //    string gen = dt.Rows[0][5].ToString();
        //    pi.view(usname,fname,address,bd,ph,gen);
        //    pi.Show();

        //}
    }
}
